package com.firewolf.hibernate.hellloworld.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;

import com.firewolf.hibernate.hellloworld.dto.News;

/**
 * @Description: 测试用例
 * @Author: 刘兴
 * @Date : 2016年10月12日
 * @package : com.firewolf.hibernate.hellloworld.test
 * @Java_Version : 1.7
 * @Version : 2016年10月12日下午5:38:00
 */
public class MainDriver {

	@Test
	public void testHelloworld() {
		// 1.创建Configuration
		Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
		// 2.创建ServiceRegistry
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
				.buildServiceRegistry();
		// 3.获取SessionFactory
		SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		// 4.获取Session
		Session session = sessionFactory.openSession();
		// 5.开启事务
		Transaction transaction = session.beginTransaction();

		// 6.处理数据库操作
		News news = new News("中央新闻", "刘兴", new Date());
		session.save(news);
		
		// 7.提交事务
		transaction.commit();
		// 8.关闭session
		session.close();
		// 9.关闭SessionFactory
		sessionFactory.close();

	}
}
